﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using StillVoiceCommon;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace TeamTime
{
    public partial class LeftMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

            }
        }

        public void buildMenu()
        {
            Menu1.Items.Clear();
            DataSet ds = GetMenuDataSet();//get a dataset of menu items for the current user
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                #region loop thru the dataset creating menu items
                foreach (DataRow orow in ds.Tables["Level1"].Rows)
                {
                    MenuItem omnu = new MenuItem();
                    omnu.Text = Functions.nz(orow["MenuItem"], "-").ToString();
                    omnu.NavigateUrl = Functions.nz(orow["Target"], "").ToString();
                    //if NZ(orow["addSeparatorBefore"), False) {
                    //    ossep As New MenuItem
                    //    ossep.SeparatorImageUrl = "images/separator.gif"
                    //    Menu1.ChildItems.Add(ossep)
                    //}
                    if (Functions.nz(orow["ImageFileName"], "").ToString() != "")
                    {
                        omnu.ImageUrl = orow["ImageFileName"].ToString();
                    }
                    Menu1.Items.Add(omnu);
                    foreach (DataRow detailRow in orow.GetChildRows("Level1.2"))
                    {
                        MenuItem omnu2 = new MenuItem();
                        omnu2.Text = Functions.nz(detailRow["MenuItem"], "-").ToString();
                        omnu2.NavigateUrl = Functions.nz(detailRow["Target"], "").ToString();
                        //if NZ(detailRow["addSeparatorBefore"], False) {
                        //    ossep As New MenuItem
                        //    ossep.SeparatorImageUrl = "images/separator.gif"
                        //    omnu.ChildItems.Add(ossep)
                        //}
                        if (Functions.nz(detailRow["ImageFileName"], "").ToString() != "")
                        {
                            omnu2.ImageUrl = detailRow["ImageFileName"].ToString();
                        }
                        omnu.ChildItems.Add(omnu2);

                        foreach (DataRow oRow2 in detailRow.GetChildRows("Level2.3"))
                        {
                            MenuItem omnu3 = new MenuItem();
                            omnu3.Text = Functions.nz(oRow2["MenuItem"], "-").ToString();
                            omnu3.NavigateUrl = Functions.nz(oRow2["Target"], "").ToString();
                            //if NZ(oRow2["addSeparatorBefore"), False) {
                            //    ossep As New MenuItem
                            //    ossep.SeparatorImageUrl = "images/separator.gif"
                            //    omnu2.ChildItems.Add(ossep)
                            //}
                            if (Functions.nz(oRow2["ImageFileName"], "").ToString() != "")
                            {
                                omnu3.ImageUrl = oRow2["ImageFileName"].ToString();
                            }
                            omnu2.ChildItems.Add(omnu3);
                            //Menu1.Items(Menu1.Items.Count - 1).ChildItems.Add(omnu2)
                        }
                    }
                #endregion loop thru the dataset creating menu items
                }
                this.tblAdminMenu.Visible = true;
            }
            else
            {
                this.tblAdminMenu.Visible = false;
            }

        }
        private DataSet GetMenuDataSet()
        {

            DataSet ds = (DataSet)Session["MenuDS"]; //Get this user's menu from Session
            if (ds == null) //not in Session, fetch from db
            {
                #region fetch menu hierarchy from db
                ds = new DataSet();
                System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter();
                string sql;

                sql = "select distinct Target, MenuItem, ID, ParentMenuItemID, [Order], UserID, ImageFileName, Level " +
                    " from [Security].ViewUserMenuItems where UserID = " + Session["UserID"] +
                    " and Level = 1 " +
                    " order by [Order]";

                string[] ar = { "Level1" };
                SqlHelper.FillDataset(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql, ds, ar);

                sql = "select distinct Target, MenuItem, ID, ParentMenuItemID, [Order], UserID, ImageFileName " +
                    " from [Security].ViewUserMenuItems where UserID = " + Session["UserID"] +
                    " and Level = 2 " +
                    " order by [Order]";
                da = sqlDB.getDA(sql);
                da.Fill(ds, "Level2");

                sql = "select distinct Target, MenuItem, ID, ParentMenuItemID, [Order], UserID, ImageFileName " +
                    " from [Security].ViewUserMenuItems where UserID = " + Session["UserID"] +
                    " and Level = 3 " +
                    " order by [Order]";
                da = sqlDB.getDA(sql);
                da.Fill(ds, "Level3");

                //Define relations
                DataRelation r1 = ds.Relations.Add("Level1.2", ds.Tables["Level1"].Columns["ID"], ds.Tables["Level2"].Columns["ParentMenuItemID"]);
                r1.Nested = true;

                DataRelation r2 = ds.Relations.Add("Level2.3", ds.Tables["Level2"].Columns["ID"], ds.Tables["Level3"].Columns["ParentMenuItemID"]);
                r2.Nested = true;
                #endregion fetch menu hierarchy from db
                Session.Add("MenuDS", ds);
            }
            return ds;
        }

    }
}